<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

class PageFactory {
	
	private $db_access;

	public function __construct() {
		$this->db_access = new DbAccessor();
	}

	public function get() {
		global $CONFIG;
		$page = new Page();
		$path = $_SERVER['PATH_INFO'];

		if (ereg("^/?$", $path)) {
			redirect($CONFIG['homedir']);
		}
		
		$page->full_path = $path;
		$page->dir_path = ereg_replace("/$","",ereg("\.",$path)?dirname($path):$path);

		$dirs = explode("/",substr($page->dir_path, 1));	

		$parent = 0;
		
		for ($i = 0; $i < count($dirs); $i++) {
			
			$dir = $dirs[$i];
		
			$sql = $this->db_access->db_query_select(array(array("*"), array("parent_id"), array("sorting_name", "sorting_sql")), 
												   array("DIRECTORY", "DIRECTORY_TREE", "SORTING"), 
													 "DIRECTORY.dir_id = DIRECTORY_TREE.child_id AND DIRECTORY_TREE.parent_id = $parent " . 
													 "AND DIRECTORY.name = '$dir' AND DIRECTORY.sorting_id = SORTING.sorting_id");
			
			$sqlresult = $this->db_access->db_do_query($sql);
			
			if ($this->db_access->db_num_rows($sqlresult) != 1) {
				errorpage(2);
			}
				
			$result = $this->db_access->db_fetch_array($sqlresult);
			
			$page->directory_tree[] = $result;

			$parent = $result['dir_id'];
		}
		
		$page->dir_depth = count($page->directory_tree);
		
		if (ereg("\.", $path)) {
			$page->page = basename($path);
			$page->type = substr(strstr($page->page,"."),1);
			$page->id = substr($page->page,0,strpos($page->page,"."));
			$page->pageType = $this->get_page_type($page->type, $page);
		} else {
			$page->pageType = new DirPage();
		}
		
		$THISDIR = $page->directory_tree[$page->dir_depth - 1];

		$main = $THISDIR['body_main'] ? $THISDIR['body_main'] : -1;
		$footer = $THISDIR['body_footer'] ? $THISDIR['body_footer'] : -1;
		
		$sql = $this->db_access->db_query_select(array(array("body"), "(body_id = $main) as is_main"), array("BODY", "DUMMY"), "body_id IN ($main,$footer)");
		$sqlresult = $this->db_access->db_do_query($sql);
		$page->body_main_id = $main;
		$page->body_footer_id = $footer;
		while ($b = $this->db_access->db_fetch_array($sqlresult)) {
			if ($b['is_main']) {
				$page->body_main = $this->db_access->db_desqlize($b['body']);
			} else {
				$page->body_footer = $this->db_access->db_desqlize($b['body']);
			}
		}
		return $page;
	}

	private function get_page_type($type, $page)
	{
		switch ($type) {
			case 'login':
				return new LoginPage($page);
			case 'gallery':
				return new GalleryPage($page);
			case 'article':
				return new ArticlePage($page);
			case 'access':
				return new AccessPage($page);
			case 'activate':
				return new ActivatePage($page);
			case 'add_item':
				return new AddItemPage($page);
			case 'admin':
				return new AdminPage($page);
			case 'ad' : 
				return new AdPage($page);
			case 'art' : 
				return new ArtPage($page);
			case 'colors' :
				return new ColorsPage($page);
			case 'comment' :
				return new CommentPage($page);
			case 'delete' :
				return new DeletePage($page);
			case 'dir' :
				return new DirPage($page);
			case 'downer':
				return new DownerPage($page);
			case 'download':
				return new DownloadPage($page);
			case 'topic':
				return new TopicPage($page);
			case 'news':
				return new NewsPage($page);
			case 'mailbox':
				return new MailBoxPage($page);
			case 'poll':
				return new PollPage($page);
			case 'link':
				return new LinkPage($page);
			case 'fiche':
				return new FichePage($page);
			case 'profile':
				return new ProfilePage($page);
			case 'event':
				return new EventPage($page);
			case 'register':
				return new RegisterPage($page);
		}
		throw new Exception("unknown page type: $type");
	}
}

?>
